<?php

$conn = pg_connect("host=localhost port=5432 dbname=sad user=usersad password=usersad"); 

echo "<HTML><HEAD></HEAD><BODY>";
echo "Iniciando importa&ccedil;&atilde;o...<BR />";


$handle = fopen('cenario1-salas.csv', 'r');

if ($handle) {
	 $linha = 0;
	 echo "Importando salas...<br><br>\n";
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        if($linha>0 && strlen( $buffer) > 1 ){
//        		echo $buffer . "<br/>\n";
		  		$array = spliti(",", $buffer);

				$stringQuery = "INSERT INTO tb_salas (sala,numero_cadeiras,acessivel,qualidade) VALUES ('".$array[0]."','".$array[1]."','".$array[2]."','".$array[3]."' );";

		  		echo "linha: " . $linha . " - inserido: ".$stringQuery;

				$result = pg_query($conn, $stringQuery );
				if (!$result) {
  					echo " Erro";
				}else {
					echo " ok";
				}
		  		echo "<br/>\n";
		  }
		  $linha++;
    }
    fclose($handle);
}



$handle = fopen('cenario1-turmas.csv', 'r');

if ($handle) {
	 $linha = 0;
	 $countHorarios=0;
	 echo "<br><br>\nImportando turmas...<br><br>\n";
  	 echo "<table border=1><tr><td></td><td>disciplina</td><td>professor</td><td>dias_horario</td><td>numero_alunos</td><td>curso</td><td>periodo</td><td>acessibilidade</td><td>qualidade</td></tr>";				
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        if($linha>0 && strlen( $buffer) > 1 ){
//        		echo $buffer . "<br/>\n";
//		  		echo "linha: " . $linha . " - inserido: ";
		  		$array = spliti(",", $buffer);

//horario == $array[2] 

//   *****  verificar as colunas da tabela de TURMAS do banco    

				$stringQuery = "INSERT INTO tb_turmas (disciplina,professor,numero_alunos,curso,periodo,acessibilidade,qualidade) VALUES ('".$array[0]."','".$array[1]."','".$array[3]."','".$array[4]."','".$array[5]."','".$array[6]."','".$array[7]."' )";				
				$result = pg_query($conn, $stringQuery );

          // apenas debug 
        echo "<tr><td colspan=9>".$stringQuery;
				if (!$result) {
  					echo " Erro";
//  					exit;
				}else {
					echo " ok";
				}
				echo "</td></tr>";

				$stringQuery = "SELECT id FROM tb_turmas ORDER BY id DESC LIMIT 1;";				
//horario ,'".$array[2]."'
				$result = pg_query($conn, $stringQuery );
				if (!$result) {
  					echo " Erro\n";
				}
				$row = pg_fetch_row($result);
				
				$ultimo_id_turmas = $row[0];

				
//		  		echo $stringQuery ."<br/>\n";


				echo "<tr><td>".$linha."</td><td>".$array[0]."</td><td>".$array[1]."</td><td>";
				$horario = spliti("-", $array[2]);
				echo $array[2]."<br />\n";
				for($i=0; $i< count( $horario );$i++ ) {
//					 echo $horario[$i] . "<br />";
					switch( substr( $horario[$i],0,1) ) {
						case 2: echo "SEG ";break;
						case 3: echo "TER ";break;
						case 4: echo "QUA ";break;
						case 5: echo "QUI ";break;
						case 6: echo "SEX ";break;
						case 7: echo "SAB ";break;
					}
					switch( strtolower( substr( $horario[$i],1,1) ) ) {
						case 'm': $hora = 6;
						break;
						case 't': $hora = 12;
						break;
						case 'n': $hora = 18;
						break;
					}
					echo  ($hora + substr( $horario[$i],2,1)) .":00";
					if( (substr($horario[$i],3,1))!=null ) 
						echo  " - ". ($hora + substr( $horario[$i],3,1)) .":00 "; 
					$stringQuery = "INSERT INTO tb_horarios (id_sala, id_turma, horario) VALUES ('1', '".$ultimo_id_turmas."', '".$horario[$i]."' )";				
					$result = pg_query($conn, $stringQuery );
					if (!$result) {
  						echo " Erro";
//  						exit;
					}else {
						echo " ok";
					}
					echo "<br />\n";
					$countHorarios++;
				}
				
				
				echo "</td><td>".$array[3]."</td><td>".$array[4]."</td><td>".$array[5]."</td><td>".$array[6]."</td><td>".$array[7]."</td></tr>";				
//		  		echo "linha: " . $linha . " - inserido: ".$stringQuery;
				
		  }
		  $linha++;
    }
    fclose($handle);
}
echo "</table>";
echo "<br />Importado ".($linha-2)." turmas e ".$countHorarios." hor&aacute;rios<br />";
echo "Importa&ccedil;&atilde;o conclu&iacute;da.<BR />";
echo "</BODY></HTML>";


?>